package nus.mtech.presentation;

import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.border.EtchedBorder;

import nus.mtech.constant.PresentationConstant;


/**
 * Import Data Panel 
 *
 * @author Alvin Lee
 * 
 */
final class ExportPanel extends JPanel implements ActionListener {

    // Automatically generated variable: serialVersionUID
    private static final long serialVersionUID = 9153854991399196326L;

    // Screen dimension object
    private Dimension screen = null;

    private JTextField txtCustomerFile;
    private JButton btnBrowseCustomerFile;
    private JTextField txtSubscriptionFile;
    private JButton btnBrowseSubscriptionFile;
    private JButton btnExportSubscription;
    private JButton btnBrowseCallFile;
    private JButton btnExportCall;
    private JButton btnImportCustomer;
    private JTextField txtCallFile;
    private JFileChooser fileChooser;
    private JButton btnBrowseBillFile;
    private JButton btnExportBill;
    private JTextField txtBillFile;

    /**
     * HomePanel Constructor.
     */
    ExportPanel() {
        super(null);
        this.setBackground(PresentationConstant.CONSOLE_BACKGROUND);
        this.setPreferredSize(new Dimension(1004, 643));
        initialization();

    }

    /**
     * Creates Batch Console
     * 
     * @param user
     *            UserLogin
     */
    public void initialization() {
        try {
            removeAll();
            fileChooser = new JFileChooser();
            watch(fileChooser);
            screen = Toolkit.getDefaultToolkit().getScreenSize();

            final JLabel importDataPanelLabel = new JLabel();
            importDataPanelLabel
            .setHorizontalAlignment(SwingConstants.CENTER);
            importDataPanelLabel.setFont(new Font("", Font.BOLD, 16));
            importDataPanelLabel.setText("Export Data");
            importDataPanelLabel.setBounds(0, 10, 1004, 21);
            add(importDataPanelLabel);

            JLabel borderBase = new JLabel();
            borderBase.setBorder(new EtchedBorder(EtchedBorder.LOWERED));
            borderBase.setBounds(40, 71, 944, 61);
            add(borderBase);

            JLabel lbl_CustomerHeader = new JLabel("Customer Information");
            lbl_CustomerHeader.setFont(new Font("Dialog", Font.BOLD, 14));
            lbl_CustomerHeader.setBounds(40, 44, 242, 21);
            add(lbl_CustomerHeader);

            JLabel lblCustomerFile = new JLabel();
            lblCustomerFile.setText("Customer File:");
            lblCustomerFile.setHorizontalAlignment(SwingConstants.LEFT);
            lblCustomerFile.setBounds(88, 88, 146, 21);
            add(lblCustomerFile);

            txtCustomerFile = new JTextField();
            txtCustomerFile.setEnabled(false);
            txtCustomerFile.setBounds(245, 91, 458, 21);
            add(txtCustomerFile);

            JLabel borderFileCopy = new JLabel();
            borderFileCopy.setBorder(new EtchedBorder(EtchedBorder.LOWERED));
            borderFileCopy.setBounds(40, 180, 944, 61);
            add(borderFileCopy);

            btnBrowseCustomerFile = new JButton();
            btnBrowseCustomerFile.setText("Browse");
            btnBrowseCustomerFile.setBounds(755, 88, 106, 26);
            btnBrowseCustomerFile.addActionListener(this);
            add(btnBrowseCustomerFile);

            JLabel lblSubscriptionInfo = new JLabel("Subscription Information");
            lblSubscriptionInfo.setFont(new Font("Dialog", Font.BOLD, 14));
            lblSubscriptionInfo.setBounds(40, 153, 242, 21);
            add(lblSubscriptionInfo);

            JLabel lblSubscriptionFile = new JLabel();
            lblSubscriptionFile.setText("Subscription File:");
            lblSubscriptionFile.setHorizontalAlignment(SwingConstants.LEFT);
            lblSubscriptionFile.setBounds(88, 203, 146, 21);
            add(lblSubscriptionFile);

            txtSubscriptionFile = new JTextField();
            txtSubscriptionFile.setEnabled(false);
            txtSubscriptionFile.setBounds(245, 203, 458, 21);
            add(txtSubscriptionFile);

            btnBrowseSubscriptionFile = new JButton();
            btnBrowseSubscriptionFile.setText("Browse");
            btnBrowseSubscriptionFile.setBounds(755, 200, 106, 26);
            btnBrowseSubscriptionFile.addActionListener(this);
            add(btnBrowseSubscriptionFile);

            btnExportSubscription = new JButton();
            btnExportSubscription.setText("Export");
            btnExportSubscription.setBounds(871, 200, 106, 26);
            btnExportSubscription.addActionListener(this);
            add(btnExportSubscription);

            JLabel lbl_DirCopy = new JLabel();
            lbl_DirCopy.setBorder(new EtchedBorder(EtchedBorder.LOWERED));
            lbl_DirCopy.setBounds(40, 291, 944, 61);
            add(lbl_DirCopy);

            JLabel lbl_CallFile = new JLabel("Call Information");
            lbl_CallFile.setFont(new Font("Dialog", Font.BOLD, 14));
            lbl_CallFile.setBounds(40, 262, 242, 21);
            add(lbl_CallFile);

            JLabel lblCallFile = new JLabel();
            lblCallFile.setText("Call File:");
            lblCallFile.setHorizontalAlignment(SwingConstants.LEFT);
            lblCallFile.setBounds(88, 314, 160, 21);
            add(lblCallFile);

            txtCallFile = new JTextField();
            txtCallFile.setEnabled(false);
            txtCallFile.setBounds(245, 314, 458, 21);
            add(txtCallFile);

            btnBrowseCallFile = new JButton();
            btnBrowseCallFile.setText("Browse");
            btnBrowseCallFile.setBounds(755, 311, 106, 26);
            btnBrowseCallFile.addActionListener(this);
            add(btnBrowseCallFile);

            btnExportCall = new JButton();
            btnExportCall.setText("Export");
            btnExportCall.setBounds(867, 311, 106, 26);
            btnExportCall.addActionListener(this);   
            add(btnExportCall);

            JLabel lbl_cmbox = new JLabel();
            lbl_cmbox.setBorder(new EtchedBorder(EtchedBorder.LOWERED));
            lbl_cmbox.setBounds(40, 410, 944, 61);
            add(lbl_cmbox);
            //  Label4          
            JLabel lbl_BillInfo = new JLabel("Bill Information");
            lbl_BillInfo.setFont(new Font("Dialog", Font.BOLD, 14));
            lbl_BillInfo.setBounds(40, 378, 305, 21);
            add(lbl_BillInfo);
            //     lable 4 inside       
            JLabel lblBillFile = new JLabel();
            lblBillFile.setText("Bill File:");
            lblBillFile.setHorizontalAlignment(SwingConstants.LEFT);
            lblBillFile.setBounds(88, 430, 160, 21);
            add(lblBillFile);

            btnBrowseBillFile = new JButton();
            btnBrowseBillFile.setText("Browse");
            btnBrowseBillFile.setBounds(755, 427, 106, 26);
            btnBrowseBillFile.addActionListener(this);   
            add(btnBrowseBillFile);
            
            btnImportCustomer = new JButton();
            btnImportCustomer.setText("Export");
            btnImportCustomer.setBounds(867, 88, 106, 26);
            btnImportCustomer.addActionListener(this);
            add(btnImportCustomer);
            
            txtBillFile = new JTextField();
            txtBillFile.setEnabled(false);
            txtBillFile.setBounds(245, 430, 458, 21);
            add(txtBillFile);
            
            btnExportBill = new JButton();
            btnExportBill.setText("Export");
            btnExportBill.setBounds(867, 427, 106, 26);
            btnExportBill.addActionListener(this);
            add(btnExportBill);

        } catch (Exception ex) {
            System.out.println("exception");
        }
    }

    public void actionPerformed(ActionEvent actionEvent) {
        if (actionEvent.getSource() == btnBrowseCustomerFile) {
        	fileChooser.resetChoosableFileFilters();
            fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
            fileChooser.setAcceptAllFileFilterUsed(true);
            int returnVal = fileChooser.showOpenDialog(this);

            if (returnVal == JFileChooser.APPROVE_OPTION) {
            	txtCustomerFile.setText(fileChooser.getSelectedFile().getPath());
            }
        }
        else if (actionEvent.getSource() == btnBrowseSubscriptionFile) {
        	fileChooser.resetChoosableFileFilters();
            fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
            fileChooser.setAcceptAllFileFilterUsed(true);
            int returnVal = fileChooser.showOpenDialog(this);

            if (returnVal == JFileChooser.APPROVE_OPTION) {
            	txtSubscriptionFile.setText(fileChooser.getSelectedFile().getPath());
            }
        }
        else if (actionEvent.getSource() == btnBrowseCallFile) {
        	fileChooser.resetChoosableFileFilters();
            fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
            fileChooser.setAcceptAllFileFilterUsed(true);
            int returnVal = fileChooser.showOpenDialog(this);

            if (returnVal == JFileChooser.APPROVE_OPTION) {
            	txtCallFile.setText(fileChooser.getSelectedFile().getPath());
            }
        }
        else if (actionEvent.getSource() == btnBrowseBillFile) {
        	fileChooser.resetChoosableFileFilters();
            fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
            fileChooser.setAcceptAllFileFilterUsed(true);
            int returnVal = fileChooser.showOpenDialog(this);

            if (returnVal == JFileChooser.APPROVE_OPTION) {
            	txtBillFile.setText(fileChooser.getSelectedFile().getPath());
            }
        }
    }

    private void watch(Container c) {
        int n = c.getComponentCount();
        for (int i = 0; i < n; i++) {
            Component ch = c.getComponent(i);
            if (ch instanceof Container) {
                Container child = (Container) ch;
                if (child instanceof JTextField) {
                    child.setEnabled(false);
                } else if (child instanceof JComboBox) {
                    Class jComboBoxInstance = child.getClass();
                    if ("JComboBox".equals(jComboBoxInstance.getSimpleName())) {
                        child.setEnabled(false);
                    }
                } else {
                    watch(child);
                }
            }
        }
    }
}